import { useMemo } from 'react'
import { useLocation } from 'react-router-dom'

/**
 * 自定义钩子以获取当前URL查询参数
 * @returns An object containing all query parameters
 */
export function useQuery() {
  const { search } = useLocation()

  return useMemo(() => {
    const params = new URLSearchParams(search)
    const queryParams: Record<string, string> = {}

    // Convert URLSearchParams to a plain object
    params.forEach((value, key) => {
      queryParams[key] = value
    })

    return queryParams
  }, [search])
}
